home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-03-09 | 4.3 KB | 168 lines | [TEXT/KAHL] |
- /**********************************************************************************
-
- NOTE: This file won't compile as-is, though each sample has been compiled on its
- own. The code here has been extracted from the article simply for your convenience,
- so you won't have to type it in. Take what you need, and adapt it for your own
- devious purposes.
-
- ***********************************************************************************/
-
-
- //--------------------------------------
- // Changing fonts when using "poor man's
- // unification"
-
- #define asciiSegment 0
- #define europeanSegment 1
- #define katakanaSegment 2
- #define japaneseSegment 3
-
- short fontScript, runSegment, runScript;
- short textRunIndex, textRunCount;
-
- fontScript = Font2Script(myNewFontID);
- for (textRunIndex = 0;
- textRunIndex < textRunCount;
- textRunIndex++) {
- runSegment = textRunStore[textRunIndex].segment;
- runScript = textRunStore[textRunIndex].script;
- if (SegmentAllowedInScript(runSegment, runScript))
- textRunStore[textRunIndex].fontID = myNewFontID;
- }
-
- Boolean SegmentAllowedInScript(short segment, short script)
- {
- switch (script) {
- case smRoman:
- switch (segment) {
- case asciiSegment:
- case europeanSegment:
- return true;
- default:
- return false;
- }
- case smJapanese:
- switch (segment) {
- case asciiSegment:
- case katakanaSegment:
- case japaneseSegment:
- return true;
- default:
- return false;
- }
- default:
- switch (segment) {
- case asciiSegment:
- return true;
- default:
- return false;
- }
- }
- }
-
- //--------------------------------------
- // Determining the segment from the keyboard
- // input for "poor man's unification"
-
- #define ksJISSpace 0x8140
-
- unsigned short keyboardScript;
- unsigned short charSegment;
- EventRecord lowByteEvent;
-
- keyboardScript = GetEnvirons(smKeyScript);
- charSegment = ScriptAndByteToSegment(keyboardScript, charCode);
- if (charSegment == japaneseSegment) {
- // Get low byte of two-byte character from keyboard.
- do {
- // You can get null events between two bytes of a two-byte
- // character.
- GetNextEvent(keyDownMask | keyUpMask | autoKeyMask,
- &lowByteEvent);
- if (lowByteEvent.what == nullEvent)
- GetNextEvent(keyDownMask | keyUpMask | autoKeyMask,
- &lowByteEvent);
- } while (lowByteEvent.what == keyUp);
-
- if ((lowByteEvent.what == keyDown) ||
- (lowByteEvent.what == autoKey))
- charCode = (charCode << 8) |
- (lowByteEvent.message & charCodeMask);
- else
- // We've gotten a valid high byte under the Japanese keyboard
- // with no subsequent low byte forthcoming. Something serious is
- // wrong with the current input method. Return a Japanese space
- // for now. Hmmmm.
- charCode = ksJISSpace;
- }
-
-
- #define kASCIILow 0x00
- #define kASCIIHigh 0x7f
- #define kRange1Low 0x81
- #define kRange1High 0x9f
- #define kRange2Low 0xe0
- #define kRange2High 0xfc
-
- short ScriptAndByteToSegment(unsigned short script,
- unsigned char byte)
- {
- switch (script) {
- case smRoman:
- if ((byte >= kASCIILow) && (byte <= kASCIIHigh))
- return asciiSegment;
- else
- return europeanSegment;
- case smJapanese:
- if ((byte >= kASCIILow) && (byte <= kASCIIHigh))
- return asciiSegment;
- else if ((byte >= kRange1Low) && (byte <= kRange1High))
- return japaneseSegment;
- else if ((byte >= kRange2Low) && (byte <= kRange2High))
- return japaneseSegment;
- else
- return katakanaSegment;
- default:
- // New scripts and segments added before this.
- return asciiSegment;
- }
- }
-
- //--------------------------------------
- // "Smart" font forcing for Poor man's
- // unification.
-
- short fontScript, keyboardScript;
-
- fontScript = Font2Script(myFontID);
- keyboardScript = GetEnvirons(smKeyScript);
- // Search backward.
- if (fontScript != keyboardScript) {
- for (textRunIndex = currentRunIndex - 1;
- textRunIndex >= 0;
- textRunIndex--) {
- myFontID = textRunStore[textRunIndex].fontID;
- fontScript = Font2Script(myFontID);
- if (fontScript == keyboardScript)
- break;
- }
- }
-
- // Search forward.
- if (fontScript != keyboardScript) {
- for (textRunIndex = currentRunIndex + 1;
- textRunIndex < textRunCount;
- textRunIndex++) {
- myFontID = textRunStore[textRunIndex].fontID;
- fontScript = Font2Script(myFontID);
- if (fontScript == keyboardScript)
- break;
- }
- }
-
- // Punt if we couldn't find an appropriate run.
- if (fontScript != keyboardScript)
- myFontID = GetScript(keyboardScript, smScriptAppFond);
-
-
- }